﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dapper_Core.Common
{
    public class AsyncMailUtility: MailBase
    {
        public AsyncMailUtility() : this(false,false)
        {

        }

        public AsyncMailUtility(bool sslEnable, bool pwdCheckEnable)
        {
            smtpClient = new SmtpClient();
            smtpClient.Host = this.smtpServer;
            smtpClient.Port = this.smtpPort;
            smtpClient.EnableSsl = sslEnable;
            if (pwdCheckEnable)
            {
                smtpClient.UseDefaultCredentials = false;
                //邮箱服务没配置账号匿名
                smtpClient.Credentials = new System.Net.NetworkCredential(this.smtpAccountID, this.smtpAccountPassword);
            }
            else
            {
                smtpClient.UseDefaultCredentials = true;
            }
            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="isBodyHtml">邮件内容是否为html</param>
        /// <returns></returns>
        public bool SendMail(string to, string subject, string body, bool isBodyHtml = false)
        {
            return SendMail(to, String.Empty, subject, body, isBodyHtml);
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">收件人</param>
        /// <param name="from">发件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="isBodyHtml">邮件内容是否为html</param>
        /// <returns></returns>
        public bool SendMail(string to, string from, string subject, string body, bool isBodyHtml = false)
        {
            try
            {
                if (String.IsNullOrEmpty(from))
                {
                    from = smtpMailSender;
                }
                MailMessage message = new MailMessage();
                //to = "525882580@qq.com";
                message.To.Add(to);
                message.From = new MailAddress(from);
                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = isBodyHtml;
                message.SubjectEncoding = Encoding.UTF8;
                message.BodyEncoding = Encoding.UTF8;
                message.Priority = MailPriority.Normal;

                #region 异步发送邮件
                Task task = new Task(() =>
                {
                    smtpClient.Send(message);
                });
                task.Start(); 
                #endregion

                return true;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(MethodBase.GetCurrentMethod().DeclaringType.FullName, "异步发送邮件时出现异常", $"to={to}，from={from}，subject={subject}，body={body}，isBodyHtml={isBodyHtml}", ex, "DapperCore");
                return false;
            }
        }
    }
}
